package com.guaoran.jvm.newObject;

import java.io.*;
import java.lang.reflect.Constructor;

/**
 * @author : guaoran
 * @Description
 *  创建对象的五种方法
 * @date :2019/3/1 13:00
 */
public class CreateObject implements Cloneable ,Serializable{
    public static void main(String[] args) throws Exception {
        // new 关键字
        CreateObject o = new CreateObject();

        // Class类的newInstance
        CreateObject o1 = CreateObject.class.newInstance();

        // Constructor类的newInstance
        Constructor<CreateObject> constructor = CreateObject.class.getConstructor();
        CreateObject o2 = constructor.newInstance();

        // clone
        CreateObject o3 = (CreateObject) o.clone();

        //序列化
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("createObj"));
        out.writeObject(o);
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("createObj"));
        CreateObject o4 = (CreateObject) in.readObject();

        System.out.println(o);
        System.out.println(o1);
        System.out.println(o2);
        System.out.println(o3);
        System.out.println(o4);
    }
}
